package com.example.demo.service.impl;

import com.example.demo.mapper.ProblemFileMapper;
import com.example.demo.domain.entity.ProblemFile;
import com.example.demo.service.ProblemFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

@Service
@Transactional
public class ProblemFileServiceImpl implements ProblemFileService {


    @Value("${problemFile.dir}")
    private String FILE_DIRECTORY;

    @Autowired
    private ProblemFileMapper problemFileMapper;

    @Override
    public List<ProblemFile> getProblemFileByProblemId(Long problemId) {
        return problemFileMapper.getProblemFileByProblemId(problemId);
    }

    @Override
    public int createProblemFile(ProblemFile problemFile) {
        return problemFileMapper.createProblemFile(problemFile);
    }

    @Override
    public int deleteProblemFileById(String id) {
        ProblemFile problemFile = problemFileMapper.getProblemFileById(id);
        if (problemFile != null) {
            problemFile.getPath();
            File file = new File(FILE_DIRECTORY + problemFile.getPath());
            file.delete();
        }
        return problemFileMapper.deleteProblemFileById(id);
    }


}
